var load_more_content_link;
var article_content = document.getElementById('article');

function getNextLink(doc) {
    var more_content = doc.getElementById('more_content');
    let more = more_content.childNodes;
    if (more.length === 1) {
        load_more_content_link = more[0].href;
    } else {
        load_more_content_link = undefined;
    }
}

getNextLink(document);

window.onscroll = function () {
    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
    let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;

    if (scrollHeight > clientHeight && scrollTop + clientHeight === scrollHeight) {
        if (load_more_content_link !== undefined) {
            var http;
            if (window.XMLHttpRequest) {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                http = new XMLHttpRequest();
            } else {
                // IE6, IE5 浏览器执行代码
                http = new ActiveXObject("Microsoft.XMLHTTP");
            }

            http.open('get', load_more_content_link);
            http.send();
            http.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    let data = new DOMParser().parseFromString(http.responseText, 'text/html');
                    let nodes = data.getElementById('article').childNodes;

                    for (var i = 0; i < nodes.length; i++) {
                        var itme = nodes[i];
                        if (itme.nodeName !== '#text') {
                            article_content.appendChild(itme);
                        }
                    }
                    getNextLink(data);
                }
            }

        }
    }
}


window.onload = function () {
    var scroll_bar = document.getElementById('nav-scrollbar');
    var home = document.getElementById('home');
    var category = document.getElementById('category');
    var profile = document.getElementById('profile');
    var other = document.getElementById('other');


    var nav_elements = [home, category, profile, other]

    function nav_light(element) {
        nav_elements.forEach(function (v) {
            v.style.opacity = '1'
        });
        // element.style.opacity = '0.5'
    }

    function mscroll_bar(offset) {
        var scro_loc = scroll_bar.offsetLeft;
        var len = '';
        len = offset - scro_loc;
        scroll_bar.style.transform = 'translateX(' + len + 'px)'
    }

    scroll_bar.style.transitionDuration = '100ms'
    home.onclick = function () {
        var home_loc = home.offsetLeft;
        mscroll_bar(home_loc)
        nav_light(this)
    }

    category.onclick = function () {
        var home_loc = category.offsetLeft;
        mscroll_bar(home_loc)
        nav_light(this)
    }

    profile.onclick = function () {
        var home_loc = profile.offsetLeft;
        mscroll_bar(home_loc)
        nav_light(this)
    }

    other.onclick = function () {
        var home_loc = other.offsetLeft;
        mscroll_bar(home_loc)
        nav_light(this)
    }


    // document.body.onscroll = function () {
    //     if (window.innerWidth > 900)
    //         return;
    //     var top = document.documentElement.scrollTop
    //     var info = document.getElementById('head-content-info');
    //     var head = document.getElementById('head-content');
    //     var title = document.getElementById('head-title');
    //     var toolbar = document.getElementById('head-toolbar');
    //     if (top > 252) {
    //         head.style.height = '50px';
    //         head.style.position = 'fixed'
    //         info.style.display = 'none';
    //         title.style.display = 'none';
    //         toolbar.style.marginTop = '13px'
    //     } else {
    //         head.style.height = '300px';
    //         head.style.position = ''
    //         info.style.display = '';
    //         title.style.display = '';
    //         toolbar.style.marginTop = ''
    //     }
    // }


}